package io.mya.modules.api.webinterfz.load_cami_info.v1;

import io.mya.modules.api.webinterfz.msg.BaseResponseMsg;
import io.mya.modules.api.webinterfz.msg.load_cami_info.v1.LoadCamiInfoMsg;
import io.mya.modules.api.webinterfz.utils.SignUtils;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 查询卡密信息 V1 接口
 *
 * @author PGmm
 * @email yangmeifm@gmail.com
 * @date 2017/09/12 23:00:00
 */
@RestController
@RequestMapping("/v1")
public class LoadCamiInfoController {
    private String keys = "Zpuj8xfvb8jltBkcDvALFcEtWvgXGdxj";

    /**
     * 查询卡密信息
     */
    @RequestMapping(value = "/load_cami_info.json", method = RequestMethod.POST, consumes = {"application/json"})
    @ResponseBody
    public BaseResponseMsg load_cami_info(
            @RequestBody
                    LoadCamiInfoMsg infoMsg, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        BaseResponseMsg baseResponseMsg = new BaseResponseMsg();
        if (null == infoMsg) {
            baseResponseMsg.setCode("50000");
            baseResponseMsg.setMsg("请求信息不合法.");
            return baseResponseMsg;
        } else if (null == infoMsg.getCode() || "".equals(infoMsg.getCode())) {
            baseResponseMsg.setCode("50000");
            baseResponseMsg.setMsg("卡号信息不能为空.");
            return baseResponseMsg;
        }
        Map<String, Object> map = ( Map<String, Object>)JSONObject.toBean(JSONObject.fromObject(infoMsg), Map.class);
        boolean verify = SignUtils.sing(map, keys);
        if (!verify) {
            baseResponseMsg.setCode("50000");
            baseResponseMsg.setMsg("验签失败.");
            return baseResponseMsg;
        }
        //查询数据库
        return baseResponseMsg;
    }

}
